package com.company;

import lombok.extern.slf4j.Slf4j;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @date : 2023-03-23 下午7:47
 * JVM参数
 * -Xms2g -Xmx2g -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps
 * 1亿行数据，时间9秒
 */

@Slf4j
public class P048_ReadBigFileWithStream {

    public static void main(String[] args) {
        try {
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String fileName = "file/new.txt";
        long start = System.nanoTime();
        List<String> list = new ArrayList<>();
        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
            stream.forEach(s -> {
                if (s.contains("99999999")) {
                    list.add(s);
                }
            });
        } catch (Exception e) {
            log.error("文件读取异常：", e);
        }
        log.info("list:{}", list);
        long time = System.nanoTime() - start;
        log.info("读取文件数据耗时：{} s", time / 1000.0 / 1000 / 1000);
    }

}
